package test.study.nosql.mongodb.client;

import cn.hutool.core.util.StrUtil;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;

@Slf4j
public class MgodbClient {

    //

    public static MongoClient client() {
        MongoClient mongoClient = MongoClients.create(url);
        return mongoClient;
    }

    public static MongoClient clientV1() {
        //
        MongoCredential credential = MongoCredential.createScramSha1Credential(username, "test", password.toCharArray());
        //
        MongoClientSettings settings = MongoClientSettings.builder()
                .credential(credential)
                .build();
        //
        MongoClient client = MongoClients.create(settings);
        return client;
    }

    public static MongoClient clientV2() {
        //
        ConnectionString connStr = new ConnectionString(url);
        MongoClient client = MongoClients.create(connStr);
        return client;
    }


    public static MongoCollection getCollection(String collectionName) {
        MongoClient client = MgodbClient.clientV2();
        MongoDatabase db = client.getDatabase("xpaper");
        MongoCollection<Document> collection = db.getCollection(collectionName);
        return collection;
    }
}
